﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleAppDemo.LinqDemo
{
    public static class LinqBaseOperation
    {
        public static void Entry()
        {
            //执行该查询时，将生成第一次测验中分数高于 90 分的所有学生的列表
            var moreThan90 = from student in _students
                             where student.Scores[0] > 90
                             orderby student.Last ascending 
                             select student;
            Console.Write("More than 90 the student name and score");
            foreach (var student in moreThan90)
            {
                Console.WriteLine("{0},{1} :{2}",student.Last,student.First,student.Scores[0]);
            }

            //新查询使用学生的姓的第一个字母作为关键字对学生进行分组
            var groupStudents = from student in _students
                                group student by student.Last[0] into groupList
                                orderby groupList.Key ascending 
                                select groupList; ;
                               
            Console.Write("-------group by the students by using the first letter--");
            foreach (var group in groupStudents)
            {
                Console.WriteLine("key:{0}", group.Key);
                foreach (var student in group)
                {
                    Console.WriteLine("{0},{1}", student.Last, student.First);
                }
               
            }
            //新查询计算各测试成绩的平均分小于第一成绩的人员列表
            Console.WriteLine("-----Average score less than the first score----");
            var totalSorceList = from student in _students
                                 let totalScore = student.Scores[0] + student.Scores[1] +
                                                  student.Scores[2] + student.Scores[3]
                                 where totalScore/4 < student.Scores[0]
                                 select student;
            foreach (var student in totalSorceList)
            {
                Console.WriteLine("{0},{1}",student.Last,student.First);
            }

        }

        #region Data and Struct 
        public class Student
        {
            public string First { get; set; }
            public string Last { get; set; }
            public int ID { get; set; }
            public List<int> Scores;
        }

        // Create a data source by using a collection initializer.
        static List<Student> _students = new List<Student>
        {
           new Student {First="Svetlana", Last="Omelchenko", ID=111, Scores= new List<int> {97, 92, 81, 60}},
           new Student {First="Claire", Last="O’Donnell", ID=112, Scores= new List<int> {75, 84, 91, 39}},
           new Student {First="Sven", Last="Mortensen", ID=113, Scores= new List<int> {88, 94, 65, 91}},
           new Student {First="Cesar", Last="Garcia", ID=114, Scores= new List<int> {97, 89, 85, 82}},
           new Student {First="Debra", Last="Garcia", ID=115, Scores= new List<int> {35, 72, 91, 70}},
           new Student {First="Fadi", Last="Fakhouri", ID=116, Scores= new List<int> {99, 86, 90, 94}},
           new Student {First="Hanying", Last="Feng", ID=117, Scores= new List<int> {93, 92, 80, 87}},
           new Student {First="Hugo", Last="Garcia", ID=118, Scores= new List<int> {92, 90, 83, 78}},
           new Student {First="Lance", Last="Tucker", ID=119, Scores= new List<int> {68, 79, 88, 92}},
           new Student {First="Terry", Last="Adams", ID=120, Scores= new List<int> {99, 82, 81, 79}},
           new Student {First="Eugene", Last="Zabokritski", ID=121, Scores= new List<int> {96, 85, 91, 60}},
           new Student {First="Michael", Last="Tucker", ID=122, Scores= new List<int> {94, 92, 91, 91} }
        };
        #endregion
    }


}
